# -*- coding: utf-8 
from EssaiAVA.ava.models import * 
from django.core.mail import send_mail
from django.utils.translation import ugettext as _

BASE_PATH = "/home/Kondine/PT AVA 1.0/AVA 1.5/"

## Messages restaurant

def message_invitation_restaurant(invit, invite_id):
    invite = Invite.objects.get(id=invite_id)
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_invitation_restaurant.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/choix/{}"
    lien = url_string.format(str(invit.id),str(invite_id))
    return string.format(jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         an=str(invit.date.year),
                         heure=str(invit.heure),
                         email=str(invit.invite_set.all()[0].email),
                         nom=str(invit.invite_set.all()[0]),
                         lien=lien)
    
def message_information_restaurant(invit, invite, orga_id):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_information_restaurant.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/manage/{}".format(str(invit.id), str(orga_id))
    return string.format(email=str(invite.email),
                         jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         lien=url_string
                         )

def message_organisateur_restaurant(invit, orga_id):
    invite = Invite.objects.get(id=orga_id)
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_organisateur_restaurant.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/manage/{}".format(str(invit.id), str(orga_id))
    return string.format(jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         lien=url_string
                         )
    
def message_invite_restaurant(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_invite_restaurant.txt")
    string = _(message.read())
    url_string = "http://127.0.0.1:8000/restau/{}/manage/{}/".format(str(invit.id), str(invite.id))
    return string.format(jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         lien=url_string)    
    
def message_resultat_restaurant(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_resultat_restaurant.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/resultat/{}/".format(str(invit.id), str(invite.id))
    return string.format(jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         lien=url_string,
                         nom=str(invit.invite_set.all()[0]),
                         email=str(invit.invite_set.all()[0].email)                         
                         )
    
def envoyer_message_resultat(invit):
    invite_present = []
    for invite in invit.invite_set.all():
        if invite.presence == True:
            invite_present.append(invite)
            send_mail("Invitation OPTAMI",
                            message_resultat_restaurant(invit, invite),
                            "invitation.ava@gmail.com",
                            [str(invite.email)],
                            fail_silently=False)
            
def message_rappel_restaurant(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_rappel_restaurant.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/choix/{}"
    return string.format(jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         an=str(invit.date.year),
                         nom=str(invit.invite_set.all()[0]),
                         lien=url_string.format(str(invit.id),str(invite.id)),
                         email=str(invit.invite_set.all()[0].email)                         
                         )

def message_confirm_restau(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_confirm_restau.txt")
    string = message.read()
    invite_present = []
    for inv in invit.invite_set.all():
        if inv.confirm==True:
            invite_present.append(invite)        
    return string.format(email=str(invite.email),
                         jour=str(invit.date.day),
                         mois=str(invit.date.month),
                         nombre=str(len(invite_present))                        
                         )

## Messages sondage

def message_invitation_sondage(invit, invite_id):
    invite = Invite.objects.get(id=invite_id)
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_invitation_sondage.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/sondage/{}/choix/{}"
    return string.format(email=str(invit.invite_set.all()[0].email),
                         nom=str(invit.invite_set.all()[0]),
                         lien=url_string.format(str(invit.id),str(invite_id)))
    
def message_information_sondage(invit, invite, orga_id):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_information_sondage.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/sondage/{}/manage/{}".format(str(invit.id), str(orga_id))
    return string.format(email=str(invite.email),
                         lien=url_string
                         )
    
def message_organisateur_sondage(invit, invite_id):
    invite = Invite.objects.get(id=invite_id)
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_organisateur_sondage.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/sondage/{}/manage/{}".format(str(invit.id), str(invite_id))
    return string.format(lien=url_string)
    
def message_invite_sondage(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_invite_sondage.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/manage/{}/".format(str(invit.id), str(invite.id))
    return string.format(lien=url_string)    
    
def message_resultat_sondage(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_resultat_sondage.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/sondage/{}/resultat/{}/".format(str(invit.id), str(invite.id))
    return string.format(lien=url_string,
                         nom=str(invit.invite_set.all()[0]),
                         email=str(invit.invite_set.all()[0].email)                         
                         )   
    
def message_rappel_sondage(invit, invite):
    message = open(BASE_PATH + "EssaiAVA/Files/" + str(invite.lang) + "/message_rappel_restaurant.txt")
    string = message.read()
    url_string = "http://127.0.0.1:8000/restau/{}/choix/{}"
    return string.format(nom=str(invit.invite_set.all()[0]),
                         lien=url_string.format(str(invit.id),str(invite.id)),
                         email=str(invit.invite_set.all()[0].email)                         
                         )